package com.sjgtw.cloud.web.largeEquipment.controller;

import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.util.EasyExcelUtil;
import com.sjgtw.cloud.web.largeEquipment.domain.LargeEquipment;
import com.sjgtw.cloud.web.largeEquipment.service.ILargeEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * equipmentController
 * 
 * @author ruoyi
 * @date 2024-03-09
 */
@RestController
@Api(tags = "1.6、路外大型器械")
@RequestMapping("/equipment")
public class LargeEquipmentController extends BaseController
{
    @Autowired
    private ILargeEquipmentService largeEquipmentService;

    /**
     * 查询equipment列表
     */
    //@RequiresPermissions("system:equipment:list")
    @GetMapping("/list")
    public TableDataInfo list(LargeEquipment largeEquipment)
    {
        startPage();
        List<Map<String,Object>> list = largeEquipmentService.selectLargeEquipmentList(largeEquipment);
        return getDataTable(list);
    }

    /**
     * 导出equipment列表
     */
   /* @RequiresPermissions("system:equipment:export")
    @Log(title = "equipment", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LargeEquipment largeEquipment)
    {
        List<LargeEquipment> list = largeEquipmentService.selectLargeEquipmentList(largeEquipment);
        ExcelUtil<LargeEquipment> util = new ExcelUtil<LargeEquipment>(LargeEquipment.class);
        util.exportExcel(response, list, "equipment数据");
    }*/

    /**
     * 获取equipment详细信息
     */
   // @RequiresPermissions("system:equipment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(largeEquipmentService.selectLargeEquipmentById(id));
    }

    /**
     * 新增equipment
     */
   // @RequiresPermissions("system:equipment:add")
    @PostMapping
    public AjaxResult add(@RequestBody LargeEquipment largeEquipment)
    {
        return toAjax(largeEquipmentService.insertLargeEquipment(largeEquipment));
    }

    /**
     * 修改equipment
     */
    //@RequiresPermissions("system:equipment:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody LargeEquipment largeEquipment)
    {
        return toAjax(largeEquipmentService.updateLargeEquipment(largeEquipment));
    }

    /**
     * 删除equipment
     */
    //@RequiresPermissions("system:equipment:remove")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(largeEquipmentService.deleteLargeEquipmentByIds(ids));
    }


    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, LargeEquipment largeEquipment)
    {
        List<LargeEquipment> list = largeEquipmentService.selectLargeEquipmentList2(largeEquipment);
        EasyExcelUtil<LargeEquipment> util = new EasyExcelUtil<>(LargeEquipment.class);
        util.exportExcel(response, list, "路外大型器械", "路外大型器械", "largeEquipment.xlsx");
    }

    /**
     * 下载模版
     */
    @ApiOperation(value = "下载模版")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response)
    {
        EasyExcelUtil<LargeEquipment> util = new EasyExcelUtil<>(LargeEquipment.class);
        util.exportExcel(response, new ArrayList<>(), "路外大型器械", "路外大型器械模版", "largeEquipment.xlsx");
    }

    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        EasyExcelUtil<LargeEquipment> util = new EasyExcelUtil<>(LargeEquipment.class);
        List<LargeEquipment> entities = util.readExcel(file, 0, 1);
        if (entities == null || entities.size() == 0) {
            return error("没有读取到数据");
        }

        List<String> errors = largeEquipmentService.check(entities);
        if (errors.size() > 0) {
            return AjaxResult.error("", errors);
        }
        largeEquipmentService.batchSave(entities);
        return success();
    }

}
